package cn.wolfcode.web.controller;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequiresPermissions(value = {"permission:list","权限列表"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("pageInfo", pageResult);
        return "permission/list";
    }
    @RequiresPermissions(value = {"permission:delete","权限删除"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if(id !=null){
            permissionService.delete(id);
        }
        return new JsonResult();
    }
    @RequiresPermissions(value = {"permission:input","权限录入"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model){
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        if (id !=null){
            Permission permission = permissionService.get(id);
            model.addAttribute("permission",permission);
        }
        return "permission/input";
    }
    @RequiresPermissions(value = {"permission:saveOrUpdate","权限编辑或者保存"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission){
        if(permission.getId() !=null){
            permissionService.update(permission);
        }else{
            permissionService.save(permission);
        }
        return "redirect:/permission/list.do";
    }
    @RequiresPermissions(value = {"permission:reload","权限加载"},logical = Logical.OR)
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        permissionService.reload();
        return new JsonResult();
    }
}
